package com.heima.admin.controller;

import com.heima.admin.entity.AdChannel;
import com.heima.admin.service.AdChannelService;
import com.heima.common.dtos.PageResult;
import com.heima.common.dtos.ResponseResult;
import com.heima.model.admin.dtos.ChannelDto;
import com.heima.model.admin.dtos.ChannelListDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@Api(tags = "频道管理")
public class AdChannelController {

    @Autowired
    private AdChannelService channelService;

    /**
     * 分页查询
     * */
    @ApiOperation(value = "根据名称分页查询频道列表")
    @ApiImplicitParam(name = "dto", value = "频道请求对象", required = true, dataType = "ChannelListDto")
    @PostMapping("/api/v1/channel/list")
    public PageResult<AdChannel> list(@RequestBody ChannelListDto dto,
                                      @RequestHeader("userId") Integer userId){
        log.info("userId:{}", userId);
        return channelService.findByPage(dto);
    }

    /**
     * 新增
     * */
    @PostMapping("/api/v1/channel/save")
    public ResponseResult<Void> save(@RequestBody ChannelDto dto){
        channelService.doSave(dto);
        return ResponseResult.ok();
    }

    /**
     * 修改
     * */
    @PutMapping("/api/v1/channel/update")
    public ResponseResult<Void> update(@RequestBody ChannelDto dto){
        channelService.doUpdate(dto);
        return ResponseResult.ok();
    }

    /**
     * 删除
     * */
    @DeleteMapping("/api/v1/channel/del/{id}")
    public ResponseResult<Void> delete(@PathVariable("id") Integer id){
        channelService.doDelete(id);
        return ResponseResult.ok();
    }

    @GetMapping("/api/v1/channel/channels")
    public ResponseResult<AdChannel> channels(){
        List<AdChannel> channels = channelService.channels();
        return ResponseResult.ok(channels);
    }


}
